package com.xkh.common.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.xkh.common.common.constant.Result;
import com.xkh.common.entity.DemoData;
import com.xkh.common.service.IExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xkh
 * 2020/10/6
 */
@RestController
@RequestMapping("util/excel")
@Api(tags = "表格导出模块")
public class ExcelController {

    @Autowired
    private IExcelService excelService;


    private static List<DemoData> list;
    /**
     * 通用数据
     */
    static {
        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
    }

    @ApiOperation("表格导出")
    @PostMapping(value = "/export")
    public Result export() {
        excelService.export(list);
        return Result.succeed();
    }

    @ApiOperation("pdf导出")
    @GetMapping(value = "/pdfExport")
    public Result pdfExport(HttpServletResponse response) {
        excelService.pdfExport(response);
        return Result.succeed();
    }


}
